#ifndef __SE_WEB_PAGE_QUERY_H__
#define __SE_WEB_PAGE_QUERY_H__


#include "WordSegmentation.h"
#include "webPageOL.h"
#include"MyRedis.h"

#include <unordered_map>
#include <set>
#include <vector>
#include <string>

using std::string;
using std::vector;
using std::set;
using std::unordered_map;



class web_page_query
{
public:
    static web_page_query * get_Web_Query_ptr() {
        if(pQuery == nullptr) {
            pQuery = new web_page_query();
            atexit(destroy);
        }
        return pQuery;
    }
    static void destroy() {
        if(pQuery) {
            delete pQuery;
        }
    }
    string doQuery(const string & str);//执行查询，返回结果

    web_page_query();
    ~web_page_query();

    void loadWebAndOffset();
    void loadInvertIndexTable();

    vector<double> getQueryWordsWeightVector(std::vector<string> & queryWords);//计算查询词的权重值

    bool executeQuery(const vector<string> & queryWords,unordered_map<int, vector<double>> & resultVec);//执行查询

    vector<string> create_Json(vector<int> & docidVec, const vector<string> & queryWords);//构建json结构

    static bool ComPair(const std::pair<int, double> & lhs, const std::pair<int, double> & rhs) //比较pair大小，用于后续排序
    {
        return lhs.second > rhs.second;
    }

private:
    static web_page_query * pQuery;
    WordSegmentation _jieba;//结巴分词库对象
    unordered_map<int, webPage> _pageLib;//网页库
    unordered_map<int, std::pair<int, int>> _offsetLib;//偏移库
    unordered_map<string, set<std::pair<int, double>>> _inverIndexTable;//倒排索引表
};

#endif  //end of __SE_WEB_PAGE_QUERY_H__
